test_protocol.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. # test_protocol.py -- Tests for the git protocol
  2. # Copyright (C) 2009 Jelmer Vernooij <jelmer@jelmer.uk>
  3. #
  4. # SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
  5. # Dulwich is dual-licensed under the Apache License, Version 2.0 and the GNU
  6. # General Public License as public by the Free Software Foundation; version 2.0
  7. # or (at your option) any later version. You can redistribute it and/or
  8. # modify it under the terms of either of these two licenses.
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. #
  16. # You should have received a copy of the licenses; if not, see
  17. # <http://www.gnu.org/licenses/> for a copy of the GNU General Public License
  18. # and <http://www.apache.org/licenses/LICENSE-2.0> for a copy of the Apache
  19. # License, Version 2.0.
  20. #
  21. """Tests for the smart protocol utility functions."""
  22. from io import BytesIO
  23. from dulwich.errors import HangupException
  24. from dulwich.protocol import (
  25. MULTI_ACK,
  26. MULTI_ACK_DETAILED,
  27. SINGLE_ACK,
  28. BufferedPktLineWriter,
  29. GitProtocolError,
  30. PktLineParser,
  31. Protocol,
  32. ReceivableProtocol,
  33. ack_type,
  34. extract_capabilities,
  35. extract_want_line_capabilities,
  36. filter_ref_prefix,
  37. pkt_line,
  38. pkt_seq,
  39. )
  40. from . import TestCase
  41. class PktLineTests(TestCase):
  42. def test_pkt_line(self) -> None:
  43. self.assertEqual(b"0007bla", pkt_line(b"bla"))
  44. self.assertEqual(b"0000", pkt_line(None))
  45. def test_pkt_seq(self) -> None:
  46. self.assertEqual(b"0007bla0007foo0000", pkt_seq(b"bla", b"foo"))
  47. self.assertEqual(b"0000", pkt_seq())
  48. class FilterRefPrefixTests(TestCase):
  49. def test_filter_ref_prefix(self) -> None:
  50. self.assertEqual(
  51. {b"refs/heads/foo": b"0123456789", b"refs/heads/bar": b"0123456789"},
  52. filter_ref_prefix(
  53. {
  54. b"refs/heads/foo": b"0123456789",
  55. b"refs/heads/bar": b"0123456789",
  56. b"refs/tags/bar": b"0123456789",
  57. },
  58. [b"refs/heads/"],
  59. ),
  60. )
  61. class BaseProtocolTests:
  62. def test_write_pkt_line_none(self) -> None:
  63. self.proto.write_pkt_line(None)
  64. self.assertEqual(self.rout.getvalue(), b"0000")
  65. def test_write_pkt_line(self) -> None:
  66. self.proto.write_pkt_line(b"bla")
  67. self.assertEqual(self.rout.getvalue(), b"0007bla")
  68. def test_read_pkt_line(self) -> None:
  69. self.rin.write(b"0008cmd ")
  70. self.rin.seek(0)
  71. self.assertEqual(b"cmd ", self.proto.read_pkt_line())
  72. def test_eof(self) -> None:
  73. self.rin.write(b"0000")
  74. self.rin.seek(0)
  75. self.assertFalse(self.proto.eof())
  76. self.assertEqual(None, self.proto.read_pkt_line())
  77. self.assertTrue(self.proto.eof())
  78. self.assertRaises(HangupException, self.proto.read_pkt_line)
  79. def test_unread_pkt_line(self) -> None:
  80. self.rin.write(b"0007foo0000")
  81. self.rin.seek(0)
  82. self.assertEqual(b"foo", self.proto.read_pkt_line())
  83. self.proto.unread_pkt_line(b"bar")
  84. self.assertEqual(b"bar", self.proto.read_pkt_line())
  85. self.assertEqual(None, self.proto.read_pkt_line())
  86. self.proto.unread_pkt_line(b"baz1")
  87. self.assertRaises(ValueError, self.proto.unread_pkt_line, b"baz2")
  88. def test_read_pkt_seq(self) -> None:
  89. self.rin.write(b"0008cmd 0005l0000")
  90. self.rin.seek(0)
  91. self.assertEqual([b"cmd ", b"l"], list(self.proto.read_pkt_seq()))
  92. def test_read_pkt_line_none(self) -> None:
  93. self.rin.write(b"0000")
  94. self.rin.seek(0)
  95. self.assertEqual(None, self.proto.read_pkt_line())
  96. def test_read_pkt_line_wrong_size(self) -> None:
  97. self.rin.write(b"0100too short")
  98. self.rin.seek(0)
  99. self.assertRaises(GitProtocolError, self.proto.read_pkt_line)
  100. def test_write_sideband(self) -> None:
  101. self.proto.write_sideband(3, b"bloe")
  102. self.assertEqual(self.rout.getvalue(), b"0009\x03bloe")
  103. def test_send_cmd(self) -> None:
  104. self.proto.send_cmd(b"fetch", b"a", b"b")
  105. self.assertEqual(self.rout.getvalue(), b"000efetch a\x00b\x00")
  106. def test_read_cmd(self) -> None:
  107. self.rin.write(b"0012cmd arg1\x00arg2\x00")
  108. self.rin.seek(0)
  109. self.assertEqual((b"cmd", [b"arg1", b"arg2"]), self.proto.read_cmd())
  110. def test_read_cmd_noend0(self) -> None:
  111. self.rin.write(b"0011cmd arg1\x00arg2")
  112. self.rin.seek(0)
  113. self.assertRaises(AssertionError, self.proto.read_cmd)
  114. class ProtocolTests(BaseProtocolTests, TestCase):
  115. def setUp(self) -> None:
  116. TestCase.setUp(self)
  117. self.rout = BytesIO()
  118. self.rin = BytesIO()
  119. self.proto = Protocol(self.rin.read, self.rout.write)
  120. class ReceivableBytesIO(BytesIO):
  121. """BytesIO with socket-like recv semantics for testing."""
  122. def __init__(self) -> None:
  123. BytesIO.__init__(self)
  124. self.allow_read_past_eof = False
  125. def recv(self, size):
  126. # fail fast if no bytes are available; in a real socket, this would
  127. # block forever
  128. if self.tell() == len(self.getvalue()) and not self.allow_read_past_eof:
  129. raise GitProtocolError("Blocking read past end of socket")
  130. if size == 1:
  131. return self.read(1)
  132. # calls shouldn't return quite as much as asked for
  133. return self.read(size - 1)
  134. class ReceivableProtocolTests(BaseProtocolTests, TestCase):
  135. def setUp(self) -> None:
  136. TestCase.setUp(self)
  137. self.rout = BytesIO()
  138. self.rin = ReceivableBytesIO()
  139. self.proto = ReceivableProtocol(self.rin.recv, self.rout.write)
  140. self.proto._rbufsize = 8
  141. def test_eof(self) -> None:
  142. # Allow blocking reads past EOF just for this test. The only parts of
  143. # the protocol that might check for EOF do not depend on the recv()
  144. # semantics anyway.
  145. self.rin.allow_read_past_eof = True
  146. BaseProtocolTests.test_eof(self)
  147. def test_recv(self) -> None:
  148. all_data = b"1234567" * 10 # not a multiple of bufsize
  149. self.rin.write(all_data)
  150. self.rin.seek(0)
  151. data = b""
  152. # We ask for 8 bytes each time and actually read 7, so it should take
  153. # exactly 10 iterations.
  154. for _ in range(10):
  155. data += self.proto.recv(10)
  156. # any more reads would block
  157. self.assertRaises(GitProtocolError, self.proto.recv, 10)
  158. self.assertEqual(all_data, data)
  159. def test_recv_read(self) -> None:
  160. all_data = b"1234567" # recv exactly in one call
  161. self.rin.write(all_data)
  162. self.rin.seek(0)
  163. self.assertEqual(b"1234", self.proto.recv(4))
  164. self.assertEqual(b"567", self.proto.read(3))
  165. self.assertRaises(GitProtocolError, self.proto.recv, 10)
  166. def test_read_recv(self) -> None:
  167. all_data = b"12345678abcdefg"
  168. self.rin.write(all_data)
  169. self.rin.seek(0)
  170. self.assertEqual(b"1234", self.proto.read(4))
  171. self.assertEqual(b"5678abc", self.proto.recv(8))
  172. self.assertEqual(b"defg", self.proto.read(4))
  173. self.assertRaises(GitProtocolError, self.proto.recv, 10)
  174. def test_mixed(self) -> None:
  175. # arbitrary non-repeating string
  176. all_data = b",".join(str(i).encode("ascii") for i in range(100))
  177. self.rin.write(all_data)
  178. self.rin.seek(0)
  179. data = b""
  180. for i in range(1, 100):
  181. data += self.proto.recv(i)
  182. # if we get to the end, do a non-blocking read instead of blocking
  183. if len(data) + i > len(all_data):
  184. data += self.proto.recv(i)
  185. # ReceivableBytesIO leaves off the last byte unless we ask
  186. # nicely
  187. data += self.proto.recv(1)
  188. break
  189. else:
  190. data += self.proto.read(i)
  191. else:
  192. # didn't break, something must have gone wrong
  193. self.fail()
  194. self.assertEqual(all_data, data)
  195. class CapabilitiesTestCase(TestCase):
  196. def test_plain(self) -> None:
  197. self.assertEqual((b"bla", []), extract_capabilities(b"bla"))
  198. def test_caps(self) -> None:
  199. self.assertEqual((b"bla", [b"la"]), extract_capabilities(b"bla\0la"))
  200. self.assertEqual((b"bla", [b"la"]), extract_capabilities(b"bla\0la\n"))
  201. self.assertEqual((b"bla", [b"la", b"la"]), extract_capabilities(b"bla\0la la"))
  202. def test_plain_want_line(self) -> None:
  203. self.assertEqual((b"want bla", []), extract_want_line_capabilities(b"want bla"))
  204. def test_caps_want_line(self) -> None:
  205. self.assertEqual(
  206. (b"want bla", [b"la"]),
  207. extract_want_line_capabilities(b"want bla la"),
  208. )
  209. self.assertEqual(
  210. (b"want bla", [b"la"]),
  211. extract_want_line_capabilities(b"want bla la\n"),
  212. )
  213. self.assertEqual(
  214. (b"want bla", [b"la", b"la"]),
  215. extract_want_line_capabilities(b"want bla la la"),
  216. )
  217. def test_ack_type(self) -> None:
  218. self.assertEqual(SINGLE_ACK, ack_type([b"foo", b"bar"]))
  219. self.assertEqual(MULTI_ACK, ack_type([b"foo", b"bar", b"multi_ack"]))
  220. self.assertEqual(
  221. MULTI_ACK_DETAILED,
  222. ack_type([b"foo", b"bar", b"multi_ack_detailed"]),
  223. )
  224. # choose detailed when both present
  225. self.assertEqual(
  226. MULTI_ACK_DETAILED,
  227. ack_type([b"foo", b"bar", b"multi_ack", b"multi_ack_detailed"]),
  228. )
  229. class BufferedPktLineWriterTests(TestCase):
  230. def setUp(self) -> None:
  231. TestCase.setUp(self)
  232. self._output = BytesIO()
  233. self._writer = BufferedPktLineWriter(self._output.write, bufsize=16)
  234. def assertOutputEquals(self, expected) -> None:
  235. self.assertEqual(expected, self._output.getvalue())
  236. def _truncate(self) -> None:
  237. self._output.seek(0)
  238. self._output.truncate()
  239. def test_write(self) -> None:
  240. self._writer.write(b"foo")
  241. self.assertOutputEquals(b"")
  242. self._writer.flush()
  243. self.assertOutputEquals(b"0007foo")
  244. def test_write_none(self) -> None:
  245. self._writer.write(None)
  246. self.assertOutputEquals(b"")
  247. self._writer.flush()
  248. self.assertOutputEquals(b"0000")
  249. def test_flush_empty(self) -> None:
  250. self._writer.flush()
  251. self.assertOutputEquals(b"")
  252. def test_write_multiple(self) -> None:
  253. self._writer.write(b"foo")
  254. self._writer.write(b"bar")
  255. self.assertOutputEquals(b"")
  256. self._writer.flush()
  257. self.assertOutputEquals(b"0007foo0007bar")
  258. def test_write_across_boundary(self) -> None:
  259. self._writer.write(b"foo")
  260. self._writer.write(b"barbaz")
  261. self.assertOutputEquals(b"0007foo000abarba")
  262. self._truncate()
  263. self._writer.flush()
  264. self.assertOutputEquals(b"z")
  265. def test_write_to_boundary(self) -> None:
  266. self._writer.write(b"foo")
  267. self._writer.write(b"barba")
  268. self.assertOutputEquals(b"0007foo0009barba")
  269. self._truncate()
  270. self._writer.write(b"z")
  271. self._writer.flush()
  272. self.assertOutputEquals(b"0005z")
  273. class PktLineParserTests(TestCase):
  274. def test_none(self) -> None:
  275. pktlines = []
  276. parser = PktLineParser(pktlines.append)
  277. parser.parse(b"0000")
  278. self.assertEqual(pktlines, [None])
  279. self.assertEqual(b"", parser.get_tail())
  280. def test_small_fragments(self) -> None:
  281. pktlines = []
  282. parser = PktLineParser(pktlines.append)
  283. parser.parse(b"00")
  284. parser.parse(b"05")
  285. parser.parse(b"z0000")
  286. self.assertEqual(pktlines, [b"z", None])
  287. self.assertEqual(b"", parser.get_tail())
  288. def test_multiple_packets(self) -> None:
  289. pktlines = []
  290. parser = PktLineParser(pktlines.append)
  291. parser.parse(b"0005z0006aba")
  292. self.assertEqual(pktlines, [b"z", b"ab"])
  293. self.assertEqual(b"a", parser.get_tail())