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