test_protocol.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. # test_protocol.py -- Tests for the git protocol
  2. # Copyright (C) 2009 Jelmer Vernooij <jelmer@samba.org>
  3. #
  4. # This program is free software; you can redistribute it and/or
  5. # modify it under the terms of the GNU General Public License
  6. # as published by the Free Software Foundation; version 2
  7. # or (at your option) any later version of the License.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU General Public License
  15. # along with this program; if not, write to the Free Software
  16. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  17. # MA 02110-1301, USA.
  18. """Tests for the smart protocol utility functions."""
  19. from io import BytesIO
  20. from dulwich.errors import (
  21. HangupException,
  22. )
  23. from dulwich.protocol import (
  24. PktLineParser,
  25. Protocol,
  26. ReceivableProtocol,
  27. extract_capabilities,
  28. extract_want_line_capabilities,
  29. ack_type,
  30. SINGLE_ACK,
  31. MULTI_ACK,
  32. MULTI_ACK_DETAILED,
  33. BufferedPktLineWriter,
  34. )
  35. from dulwich.tests import TestCase
  36. from dulwich.tests.utils import skipIfPY3
  37. class BaseProtocolTests(object):
  38. def test_write_pkt_line_none(self):
  39. self.proto.write_pkt_line(None)
  40. self.assertEqual(self.rout.getvalue(), '0000')
  41. def test_write_pkt_line(self):
  42. self.proto.write_pkt_line('bla')
  43. self.assertEqual(self.rout.getvalue(), '0007bla')
  44. def test_read_pkt_line(self):
  45. self.rin.write('0008cmd ')
  46. self.rin.seek(0)
  47. self.assertEqual('cmd ', self.proto.read_pkt_line())
  48. def test_eof(self):
  49. self.rin.write('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('0007foo0000')
  57. self.rin.seek(0)
  58. self.assertEqual('foo', self.proto.read_pkt_line())
  59. self.proto.unread_pkt_line('bar')
  60. self.assertEqual('bar', self.proto.read_pkt_line())
  61. self.assertEqual(None, self.proto.read_pkt_line())
  62. self.proto.unread_pkt_line('baz1')
  63. self.assertRaises(ValueError, self.proto.unread_pkt_line, 'baz2')
  64. def test_read_pkt_seq(self):
  65. self.rin.write('0008cmd 0005l0000')
  66. self.rin.seek(0)
  67. self.assertEqual(['cmd ', 'l'], list(self.proto.read_pkt_seq()))
  68. def test_read_pkt_line_none(self):
  69. self.rin.write('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('0100too short')
  74. self.rin.seek(0)
  75. self.assertRaises(AssertionError, self.proto.read_pkt_line)
  76. def test_write_sideband(self):
  77. self.proto.write_sideband(3, 'bloe')
  78. self.assertEqual(self.rout.getvalue(), '0009\x03bloe')
  79. def test_send_cmd(self):
  80. self.proto.send_cmd('fetch', 'a', 'b')
  81. self.assertEqual(self.rout.getvalue(), '000efetch a\x00b\x00')
  82. def test_read_cmd(self):
  83. self.rin.write('0012cmd arg1\x00arg2\x00')
  84. self.rin.seek(0)
  85. self.assertEqual(('cmd', ['arg1', 'arg2']), self.proto.read_cmd())
  86. def test_read_cmd_noend0(self):
  87. self.rin.write('0011cmd arg1\x00arg2')
  88. self.rin.seek(0)
  89. self.assertRaises(AssertionError, self.proto.read_cmd)
  90. @skipIfPY3
  91. class ProtocolTests(BaseProtocolTests, TestCase):
  92. def setUp(self):
  93. TestCase.setUp(self)
  94. self.rout = BytesIO()
  95. self.rin = BytesIO()
  96. self.proto = Protocol(self.rin.read, self.rout.write)
  97. class ReceivableBytesIO(BytesIO):
  98. """BytesIO with socket-like recv semantics for testing."""
  99. def __init__(self):
  100. BytesIO.__init__(self)
  101. self.allow_read_past_eof = False
  102. def recv(self, size):
  103. # fail fast if no bytes are available; in a real socket, this would
  104. # block forever
  105. if self.tell() == len(self.getvalue()) and not self.allow_read_past_eof:
  106. raise AssertionError('Blocking read past end of socket')
  107. if size == 1:
  108. return self.read(1)
  109. # calls shouldn't return quite as much as asked for
  110. return self.read(size - 1)
  111. @skipIfPY3
  112. class ReceivableProtocolTests(BaseProtocolTests, TestCase):
  113. def setUp(self):
  114. TestCase.setUp(self)
  115. self.rout = BytesIO()
  116. self.rin = ReceivableBytesIO()
  117. self.proto = ReceivableProtocol(self.rin.recv, self.rout.write)
  118. self.proto._rbufsize = 8
  119. def test_eof(self):
  120. # Allow blocking reads past EOF just for this test. The only parts of
  121. # the protocol that might check for EOF do not depend on the recv()
  122. # semantics anyway.
  123. self.rin.allow_read_past_eof = True
  124. BaseProtocolTests.test_eof(self)
  125. def test_recv(self):
  126. all_data = '1234567' * 10 # not a multiple of bufsize
  127. self.rin.write(all_data)
  128. self.rin.seek(0)
  129. data = ''
  130. # We ask for 8 bytes each time and actually read 7, so it should take
  131. # exactly 10 iterations.
  132. for _ in range(10):
  133. data += self.proto.recv(10)
  134. # any more reads would block
  135. self.assertRaises(AssertionError, self.proto.recv, 10)
  136. self.assertEqual(all_data, data)
  137. def test_recv_read(self):
  138. all_data = '1234567' # recv exactly in one call
  139. self.rin.write(all_data)
  140. self.rin.seek(0)
  141. self.assertEqual('1234', self.proto.recv(4))
  142. self.assertEqual('567', self.proto.read(3))
  143. self.assertRaises(AssertionError, self.proto.recv, 10)
  144. def test_read_recv(self):
  145. all_data = '12345678abcdefg'
  146. self.rin.write(all_data)
  147. self.rin.seek(0)
  148. self.assertEqual('1234', self.proto.read(4))
  149. self.assertEqual('5678abc', self.proto.recv(8))
  150. self.assertEqual('defg', self.proto.read(4))
  151. self.assertRaises(AssertionError, self.proto.recv, 10)
  152. def test_mixed(self):
  153. # arbitrary non-repeating string
  154. all_data = ','.join(str(i) for i in range(100))
  155. self.rin.write(all_data)
  156. self.rin.seek(0)
  157. data = ''
  158. for i in range(1, 100):
  159. data += self.proto.recv(i)
  160. # if we get to the end, do a non-blocking read instead of blocking
  161. if len(data) + i > len(all_data):
  162. data += self.proto.recv(i)
  163. # ReceivableBytesIO leaves off the last byte unless we ask
  164. # nicely
  165. data += self.proto.recv(1)
  166. break
  167. else:
  168. data += self.proto.read(i)
  169. else:
  170. # didn't break, something must have gone wrong
  171. self.fail()
  172. self.assertEqual(all_data, data)
  173. @skipIfPY3
  174. class CapabilitiesTestCase(TestCase):
  175. def test_plain(self):
  176. self.assertEqual(('bla', []), extract_capabilities('bla'))
  177. def test_caps(self):
  178. self.assertEqual(('bla', ['la']), extract_capabilities('bla\0la'))
  179. self.assertEqual(('bla', ['la']), extract_capabilities('bla\0la\n'))
  180. self.assertEqual(('bla', ['la', 'la']), extract_capabilities('bla\0la la'))
  181. def test_plain_want_line(self):
  182. self.assertEqual(('want bla', []), extract_want_line_capabilities('want bla'))
  183. def test_caps_want_line(self):
  184. self.assertEqual(('want bla', ['la']), extract_want_line_capabilities('want bla la'))
  185. self.assertEqual(('want bla', ['la']), extract_want_line_capabilities('want bla la\n'))
  186. self.assertEqual(('want bla', ['la', 'la']), extract_want_line_capabilities('want bla la la'))
  187. def test_ack_type(self):
  188. self.assertEqual(SINGLE_ACK, ack_type(['foo', 'bar']))
  189. self.assertEqual(MULTI_ACK, ack_type(['foo', 'bar', 'multi_ack']))
  190. self.assertEqual(MULTI_ACK_DETAILED,
  191. ack_type(['foo', 'bar', 'multi_ack_detailed']))
  192. # choose detailed when both present
  193. self.assertEqual(MULTI_ACK_DETAILED,
  194. ack_type(['foo', 'bar', 'multi_ack',
  195. 'multi_ack_detailed']))
  196. @skipIfPY3
  197. class BufferedPktLineWriterTests(TestCase):
  198. def setUp(self):
  199. TestCase.setUp(self)
  200. self._output = BytesIO()
  201. self._writer = BufferedPktLineWriter(self._output.write, bufsize=16)
  202. def assertOutputEquals(self, expected):
  203. self.assertEqual(expected, self._output.getvalue())
  204. def _truncate(self):
  205. self._output.seek(0)
  206. self._output.truncate()
  207. def test_write(self):
  208. self._writer.write('foo')
  209. self.assertOutputEquals('')
  210. self._writer.flush()
  211. self.assertOutputEquals('0007foo')
  212. def test_write_none(self):
  213. self._writer.write(None)
  214. self.assertOutputEquals('')
  215. self._writer.flush()
  216. self.assertOutputEquals('0000')
  217. def test_flush_empty(self):
  218. self._writer.flush()
  219. self.assertOutputEquals('')
  220. def test_write_multiple(self):
  221. self._writer.write('foo')
  222. self._writer.write('bar')
  223. self.assertOutputEquals('')
  224. self._writer.flush()
  225. self.assertOutputEquals('0007foo0007bar')
  226. def test_write_across_boundary(self):
  227. self._writer.write('foo')
  228. self._writer.write('barbaz')
  229. self.assertOutputEquals('0007foo000abarba')
  230. self._truncate()
  231. self._writer.flush()
  232. self.assertOutputEquals('z')
  233. def test_write_to_boundary(self):
  234. self._writer.write('foo')
  235. self._writer.write('barba')
  236. self.assertOutputEquals('0007foo0009barba')
  237. self._truncate()
  238. self._writer.write('z')
  239. self._writer.flush()
  240. self.assertOutputEquals('0005z')
  241. @skipIfPY3
  242. class PktLineParserTests(TestCase):
  243. def test_none(self):
  244. pktlines = []
  245. parser = PktLineParser(pktlines.append)
  246. parser.parse("0000")
  247. self.assertEqual(pktlines, [None])
  248. self.assertEqual("", parser.get_tail())
  249. def test_small_fragments(self):
  250. pktlines = []
  251. parser = PktLineParser(pktlines.append)
  252. parser.parse("00")
  253. parser.parse("05")
  254. parser.parse("z0000")
  255. self.assertEqual(pktlines, ["z", None])
  256. self.assertEqual("", parser.get_tail())
  257. def test_multiple_packets(self):
  258. pktlines = []
  259. parser = PktLineParser(pktlines.append)
  260. parser.parse("0005z0006aba")
  261. self.assertEqual(pktlines, ["z", "ab"])
  262. self.assertEqual("a", parser.get_tail())