test_protocol.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. extract_want_line_capabilities,
  25. ack_type,
  26. SINGLE_ACK,
  27. MULTI_ACK,
  28. MULTI_ACK_DETAILED,
  29. )
  30. class ProtocolTests(TestCase):
  31. def setUp(self):
  32. self.rout = StringIO()
  33. self.rin = StringIO()
  34. self.proto = Protocol(self.rin.read, self.rout.write)
  35. def test_write_pkt_line_none(self):
  36. self.proto.write_pkt_line(None)
  37. self.assertEquals(self.rout.getvalue(), "0000")
  38. def test_write_pkt_line(self):
  39. self.proto.write_pkt_line("bla")
  40. self.assertEquals(self.rout.getvalue(), "0007bla")
  41. def test_read_pkt_line(self):
  42. self.rin.write("0008cmd ")
  43. self.rin.seek(0)
  44. self.assertEquals("cmd ", self.proto.read_pkt_line())
  45. def test_read_pkt_seq(self):
  46. self.rin.write("0008cmd 0005l0000")
  47. self.rin.seek(0)
  48. self.assertEquals(["cmd ", "l"], list(self.proto.read_pkt_seq()))
  49. def test_read_pkt_line_none(self):
  50. self.rin.write("0000")
  51. self.rin.seek(0)
  52. self.assertEquals(None, self.proto.read_pkt_line())
  53. def test_write_sideband(self):
  54. self.proto.write_sideband(3, "bloe")
  55. self.assertEquals(self.rout.getvalue(), "0009\x03bloe")
  56. def test_send_cmd(self):
  57. self.proto.send_cmd("fetch", "a", "b")
  58. self.assertEquals(self.rout.getvalue(), "000efetch a\x00b\x00")
  59. def test_read_cmd(self):
  60. self.rin.write("0012cmd arg1\x00arg2\x00")
  61. self.rin.seek(0)
  62. self.assertEquals(("cmd", ["arg1", "arg2"]), self.proto.read_cmd())
  63. def test_read_cmd_noend0(self):
  64. self.rin.write("0011cmd arg1\x00arg2")
  65. self.rin.seek(0)
  66. self.assertRaises(AssertionError, self.proto.read_cmd)
  67. class CapabilitiesTestCase(TestCase):
  68. def test_plain(self):
  69. self.assertEquals(("bla", []), extract_capabilities("bla"))
  70. def test_caps(self):
  71. self.assertEquals(("bla", ["la"]), extract_capabilities("bla\0la"))
  72. self.assertEquals(("bla", ["la"]), extract_capabilities("bla\0la\n"))
  73. self.assertEquals(("bla", ["la", "la"]), extract_capabilities("bla\0la la"))
  74. def test_plain_want_line(self):
  75. self.assertEquals(("want bla", []), extract_want_line_capabilities("want bla"))
  76. def test_caps_want_line(self):
  77. self.assertEquals(("want bla", ["la"]), extract_want_line_capabilities("want bla la"))
  78. self.assertEquals(("want bla", ["la"]), extract_want_line_capabilities("want bla la\n"))
  79. self.assertEquals(("want bla", ["la", "la"]), extract_want_line_capabilities("want bla la la"))
  80. def test_ack_type(self):
  81. self.assertEquals(SINGLE_ACK, ack_type(['foo', 'bar']))
  82. self.assertEquals(MULTI_ACK, ack_type(['foo', 'bar', 'multi_ack']))
  83. self.assertEquals(MULTI_ACK_DETAILED,
  84. ack_type(['foo', 'bar', 'multi_ack_detailed']))
  85. # choose detailed when both present
  86. self.assertEquals(MULTI_ACK_DETAILED,
  87. ack_type(['foo', 'bar', 'multi_ack',
  88. 'multi_ack_detailed']))