test_protocol.py 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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 cStringIO import StringIO
  20. from unittest import TestCase
  21. from dulwich.protocol import (
  22. Protocol,
  23. extract_capabilities,
  24. )
  25. class ProtocolTests(TestCase):
  26. def setUp(self):
  27. self.rout = StringIO()
  28. self.rin = StringIO()
  29. self.proto = Protocol(self.rin.read, self.rout.write)
  30. def test_write_pkt_line_none(self):
  31. self.proto.write_pkt_line(None)
  32. self.assertEquals(self.rout.getvalue(), "0000")
  33. def test_write_pkt_line(self):
  34. self.proto.write_pkt_line("bla")
  35. self.assertEquals(self.rout.getvalue(), "0007bla")
  36. def test_read_pkt_line(self):
  37. self.rin.write("0008cmd ")
  38. self.rin.seek(0)
  39. self.assertEquals("cmd ", self.proto.read_pkt_line())
  40. def test_read_pkt_seq(self):
  41. self.rin.write("0008cmd 0005l0000")
  42. self.rin.seek(0)
  43. self.assertEquals(["cmd ", "l"], list(self.proto.read_pkt_seq()))
  44. def test_read_pkt_line_none(self):
  45. self.rin.write("0000")
  46. self.rin.seek(0)
  47. self.assertEquals(None, self.proto.read_pkt_line())
  48. def test_write_sideband(self):
  49. self.proto.write_sideband(3, "bloe")
  50. self.assertEquals(self.rout.getvalue(), "0009\x03bloe")
  51. def test_send_cmd(self):
  52. self.proto.send_cmd("fetch", "a", "b")
  53. self.assertEquals(self.rout.getvalue(), "000efetch a\x00b\x00")
  54. def test_read_cmd(self):
  55. self.rin.write("0012cmd arg1\x00arg2\x00")
  56. self.rin.seek(0)
  57. self.assertEquals(("cmd", ["arg1", "arg2"]), self.proto.read_cmd())
  58. def test_read_cmd_noend0(self):
  59. self.rin.write("0011cmd arg1\x00arg2")
  60. self.rin.seek(0)
  61. self.assertRaises(AssertionError, self.proto.read_cmd)
  62. class ExtractCapabilitiesTestCase(TestCase):
  63. def test_plain(self):
  64. self.assertEquals(("bla", None), extract_capabilities("bla"))
  65. def test_caps(self):
  66. self.assertEquals(("bla", ["la", "la"]), extract_capabilities("bla\0la\0la"))