2
0

test_protocol.py 12 KB

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