test_protocol.py 9.5 KB

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