2
0

test_protocol.py 11 KB

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