ff2030938d45244882f4365fb60dd2549d644f46
[jelmer/dulwich-libgit2.git] / dulwich / tests / test_protocol.py
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
19
20 """Tests for the smart protocol utility functions."""
21
22
23 from cStringIO import StringIO
24 from unittest import TestCase
25
26 from dulwich.protocol import (
27     Protocol,
28     extract_capabilities,
29     extract_want_line_capabilities,
30     )
31
32 class ProtocolTests(TestCase):
33
34     def setUp(self):
35         self.rout = StringIO()
36         self.rin = StringIO()
37         self.proto = Protocol(self.rin.read, self.rout.write)
38
39     def test_write_pkt_line_none(self):
40         self.proto.write_pkt_line(None)
41         self.assertEquals(self.rout.getvalue(), "0000")
42
43     def test_write_pkt_line(self):
44         self.proto.write_pkt_line("bla")
45         self.assertEquals(self.rout.getvalue(), "0007bla")
46
47     def test_read_pkt_line(self):
48         self.rin.write("0008cmd ")
49         self.rin.seek(0)
50         self.assertEquals("cmd ", self.proto.read_pkt_line())
51
52     def test_read_pkt_seq(self):
53         self.rin.write("0008cmd 0005l0000")
54         self.rin.seek(0)
55         self.assertEquals(["cmd ", "l"], list(self.proto.read_pkt_seq()))
56
57     def test_read_pkt_line_none(self):
58         self.rin.write("0000")
59         self.rin.seek(0)
60         self.assertEquals(None, self.proto.read_pkt_line())
61
62     def test_write_sideband(self):
63         self.proto.write_sideband(3, "bloe")
64         self.assertEquals(self.rout.getvalue(), "0009\x03bloe")
65
66     def test_send_cmd(self):
67         self.proto.send_cmd("fetch", "a", "b")
68         self.assertEquals(self.rout.getvalue(), "000efetch a\x00b\x00")
69
70     def test_read_cmd(self):
71         self.rin.write("0012cmd arg1\x00arg2\x00")
72         self.rin.seek(0)
73         self.assertEquals(("cmd", ["arg1", "arg2"]), self.proto.read_cmd())
74
75     def test_read_cmd_noend0(self):
76         self.rin.write("0011cmd arg1\x00arg2")
77         self.rin.seek(0)
78         self.assertRaises(AssertionError, self.proto.read_cmd)
79
80
81 class ExtractCapabilitiesTestCase(TestCase):
82
83     def test_plain(self):
84         self.assertEquals(("bla", []), extract_capabilities("bla"))
85
86     def test_caps(self):
87         self.assertEquals(("bla", ["la"]), extract_capabilities("bla\0la"))
88         self.assertEquals(("bla", ["la"]), extract_capabilities("bla\0la\n"))
89         self.assertEquals(("bla", ["la", "la"]), extract_capabilities("bla\0la la"))
90
91     def test_plain_want_line(self):
92         self.assertEquals(("want bla", []), extract_want_line_capabilities("want bla"))
93
94     def test_caps_want_line(self):
95         self.assertEquals(("want bla", ["la"]), extract_want_line_capabilities("want bla la"))
96         self.assertEquals(("want bla", ["la"]), extract_want_line_capabilities("want bla la\n"))
97         self.assertEquals(("want bla", ["la", "la"]), extract_want_line_capabilities("want bla la la"))