test_protocol.py 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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 unittest import TestCase
  21. from dulwich.protocol import (
  22. Protocol,
  23. ReceivableProtocol,
  24. extract_capabilities,
  25. extract_want_line_capabilities,
  26. ack_type,
  27. SINGLE_ACK,
  28. MULTI_ACK,
  29. MULTI_ACK_DETAILED,
  30. )
  31. class BaseProtocolTests(object):
  32. def test_write_pkt_line_none(self):
  33. self.proto.write_pkt_line(None)
  34. self.assertEquals(self.rout.getvalue(), "0000")
  35. def test_write_pkt_line(self):
  36. self.proto.write_pkt_line("bla")
  37. self.assertEquals(self.rout.getvalue(), "0007bla")
  38. def test_read_pkt_line(self):
  39. self.rin.write("0008cmd ")
  40. self.rin.seek(0)
  41. self.assertEquals("cmd ", self.proto.read_pkt_line())
  42. def test_read_pkt_seq(self):
  43. self.rin.write("0008cmd 0005l0000")
  44. self.rin.seek(0)
  45. self.assertEquals(["cmd ", "l"], list(self.proto.read_pkt_seq()))
  46. def test_read_pkt_line_none(self):
  47. self.rin.write("0000")
  48. self.rin.seek(0)
  49. self.assertEquals(None, self.proto.read_pkt_line())
  50. def test_write_sideband(self):
  51. self.proto.write_sideband(3, "bloe")
  52. self.assertEquals(self.rout.getvalue(), "0009\x03bloe")
  53. def test_send_cmd(self):
  54. self.proto.send_cmd("fetch", "a", "b")
  55. self.assertEquals(self.rout.getvalue(), "000efetch a\x00b\x00")
  56. def test_read_cmd(self):
  57. self.rin.write("0012cmd arg1\x00arg2\x00")
  58. self.rin.seek(0)
  59. self.assertEquals(("cmd", ["arg1", "arg2"]), self.proto.read_cmd())
  60. def test_read_cmd_noend0(self):
  61. self.rin.write("0011cmd arg1\x00arg2")
  62. self.rin.seek(0)
  63. self.assertRaises(AssertionError, self.proto.read_cmd)
  64. class ProtocolTests(BaseProtocolTests, TestCase):
  65. def setUp(self):
  66. TestCase.setUp(self)
  67. self.rout = StringIO()
  68. self.rin = StringIO()
  69. self.proto = Protocol(self.rin.read, self.rout.write)
  70. class ReceivableStringIO(StringIO):
  71. """StringIO with socket-like recv semantics for testing."""
  72. def recv(self, size):
  73. # fail fast if no bytes are available; in a real socket, this would
  74. # block forever
  75. if self.tell() == len(self.getvalue()):
  76. raise AssertionError("Blocking read past end of socket")
  77. if size == 1:
  78. return self.read(1)
  79. # calls shouldn't return quite as much as asked for
  80. return self.read(size - 1)
  81. class ReceivableProtocolTests(BaseProtocolTests, TestCase):
  82. def setUp(self):
  83. TestCase.setUp(self)
  84. self.rout = StringIO()
  85. self.rin = ReceivableStringIO()
  86. self.proto = ReceivableProtocol(self.rin.recv, self.rout.write)
  87. self.proto._rbufsize = 8
  88. def test_recv(self):
  89. all_data = "1234567" * 10 # not a multiple of bufsize
  90. self.rin.write(all_data)
  91. self.rin.seek(0)
  92. data = ""
  93. # We ask for 8 bytes each time and actually read 7, so it should take
  94. # exactly 10 iterations.
  95. for _ in xrange(10):
  96. data += self.proto.recv(10)
  97. # any more reads would block
  98. self.assertRaises(AssertionError, self.proto.recv, 10)
  99. self.assertEquals(all_data, data)
  100. def test_recv_read(self):
  101. all_data = "1234567" # recv exactly in one call
  102. self.rin.write(all_data)
  103. self.rin.seek(0)
  104. self.assertEquals("1234", self.proto.recv(4))
  105. self.assertEquals("567", self.proto.read(3))
  106. self.assertRaises(AssertionError, self.proto.recv, 10)
  107. def test_read_recv(self):
  108. all_data = "12345678abcdefg"
  109. self.rin.write(all_data)
  110. self.rin.seek(0)
  111. self.assertEquals("1234", self.proto.read(4))
  112. self.assertEquals("5678abc", self.proto.recv(8))
  113. self.assertEquals("defg", self.proto.read(4))
  114. self.assertRaises(AssertionError, self.proto.recv, 10)
  115. def test_mixed(self):
  116. # arbitrary non-repeating string
  117. all_data = ",".join(str(i) for i in xrange(100))
  118. self.rin.write(all_data)
  119. self.rin.seek(0)
  120. data = ""
  121. for i in xrange(1, 100):
  122. data += self.proto.recv(i)
  123. # if we get to the end, do a non-blocking read instead of blocking
  124. if len(data) + i > len(all_data):
  125. data += self.proto.recv(i)
  126. # ReceivableStringIO leaves off the last byte unless we ask
  127. # nicely
  128. data += self.proto.recv(1)
  129. break
  130. else:
  131. data += self.proto.read(i)
  132. else:
  133. # didn't break, something must have gone wrong
  134. self.fail()
  135. self.assertEquals(all_data, data)
  136. class CapabilitiesTestCase(TestCase):
  137. def test_plain(self):
  138. self.assertEquals(("bla", []), extract_capabilities("bla"))
  139. def test_caps(self):
  140. self.assertEquals(("bla", ["la"]), extract_capabilities("bla\0la"))
  141. self.assertEquals(("bla", ["la"]), extract_capabilities("bla\0la\n"))
  142. self.assertEquals(("bla", ["la", "la"]), extract_capabilities("bla\0la la"))
  143. def test_plain_want_line(self):
  144. self.assertEquals(("want bla", []), extract_want_line_capabilities("want bla"))
  145. def test_caps_want_line(self):
  146. self.assertEquals(("want bla", ["la"]), extract_want_line_capabilities("want bla la"))
  147. self.assertEquals(("want bla", ["la"]), extract_want_line_capabilities("want bla la\n"))
  148. self.assertEquals(("want bla", ["la", "la"]), extract_want_line_capabilities("want bla la la"))
  149. def test_ack_type(self):
  150. self.assertEquals(SINGLE_ACK, ack_type(['foo', 'bar']))
  151. self.assertEquals(MULTI_ACK, ack_type(['foo', 'bar', 'multi_ack']))
  152. self.assertEquals(MULTI_ACK_DETAILED,
  153. ack_type(['foo', 'bar', 'multi_ack_detailed']))
  154. # choose detailed when both present
  155. self.assertEquals(MULTI_ACK_DETAILED,
  156. ack_type(['foo', 'bar', 'multi_ack',
  157. 'multi_ack_detailed']))